﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace CreatingA2DSprite
{
    class Wizard : hibou2.Sprite
    {
        const string WIZARD_ASSETNAME = "marche12";
        const int START_POSITION_X = 0;
        const int START_POSITION_Y = 367;
        const int WIZARD_SPEED = 160;
        const int MOVE_UP = -1;
        const int MOVE_DOWN = 1;
        const int MOVE_LEFT = -1;
        const int MOVE_RIGHT = 1;
        List<hibou2.Fireball> mFireballs = new List<hibou2.Fireball>();
        
        enum State
        {

            Walking,
            Jumping,
            Ducking

        }

        State mCurrentState = State.Walking;
        Vector2 mDirection = Vector2.Zero;
        Vector2 mSpeed = Vector2.Zero;
        KeyboardState mPreviousKeyboardState;
        Vector2 mStartingPosition = Vector2.Zero;
       
        ContentManager mContentManager;
        public void LoadContent(ContentManager theContentManager)
        {

            mContentManager = theContentManager;

            foreach (hibou2.Fireball aFireball in mFireballs)
            {
                aFireball.LoadContent(theContentManager);
            }
            Position = new Vector2(START_POSITION_X, START_POSITION_Y);
            base.LoadContent(theContentManager, WIZARD_ASSETNAME);
            Source = new Rectangle(0, 0, 150, Source.Height);
        }

        public void Update(GameTime theGameTime)
        {
            KeyboardState aCurrentKeyboardState = Keyboard.GetState();
            UpdateMovement(aCurrentKeyboardState);
            UpdateJump(aCurrentKeyboardState);
            UpdateDuck(aCurrentKeyboardState);
            UpdateFireball(theGameTime, aCurrentKeyboardState);
            mPreviousKeyboardState = aCurrentKeyboardState;
            base.Update(theGameTime, mSpeed, mDirection);

        }

        private void UpdateFireball(GameTime theGameTime, KeyboardState aCurrentKeyboardState)
        {
            foreach (hibou2.Fireball aFireball in mFireballs)
            {
                aFireball.Update(theGameTime);
            }
            if (aCurrentKeyboardState.IsKeyDown(Keys.Z) == true && mPreviousKeyboardState.IsKeyDown(Keys.Z) == false)
            {
                ShootFireball();
            }

        }

        private void ShootFireball()
        {
            if (mCurrentState == State.Walking)
            {
                bool aCreateNew = true;
                foreach (hibou2.Fireball aFireball in mFireballs)
                {
                    if (aFireball.Visible == false)
                    {
                        aCreateNew = false;
                        aFireball.Fire(Position + new Vector2(Taille.Width / 2, Taille.Height / 2),
                            new Vector2(200, 0), new Vector2(1, 0));
                        break;
                    }
                }
                if (aCreateNew == true)
                {
                    hibou2.Fireball aFireball = new hibou2.Fireball();
                    aFireball.LoadContent(mContentManager);
                    aFireball.Fire(Position + new Vector2(Taille.Width / 2, Taille.Height / 2),
                        new Vector2(200, 200), new Vector2(1, 0));
                    mFireballs.Add(aFireball);

                }

            }

        }
        private void UpdateMovement(KeyboardState aCurrentKeyboardState)
        {
            if (mCurrentState == State.Walking)
            {
                mSpeed = Vector2.Zero;
                mDirection = Vector2.Zero;
                if (aCurrentKeyboardState.IsKeyDown(Keys.Left) == true)
                {
                    mSpeed.X = WIZARD_SPEED;
                    mDirection.X = MOVE_LEFT;
                }
                else if (aCurrentKeyboardState.IsKeyDown(Keys.Right) == true)
                {
                    mSpeed.X = WIZARD_SPEED;
                    mDirection.X = MOVE_RIGHT;
                }
               /* if (aCurrentKeyboardState.IsKeyDown(Keys.Up) == true)
                {
                    mSpeed.Y = WIZARD_SPEED;
                    mDirection.Y = MOVE_UP;
                }
                else if (aCurrentKeyboardState.IsKeyDown(Keys.Down) == true)
                {
                    mSpeed.Y = WIZARD_SPEED;
                    mDirection.Y = MOVE_DOWN;
                }*/

            }

        }

        private void UpdateJump(KeyboardState aCurrentKeyboardState)
        {

            if (mCurrentState == State.Walking)
            {

                if (aCurrentKeyboardState.IsKeyDown(Keys.Space) == true && mPreviousKeyboardState.IsKeyDown(Keys.Space) == false)
                {

                    Jump();

                }

            }



            if (mCurrentState == State.Jumping)
            {
               


                if (mStartingPosition.Y - Position.Y > 100)
                {

                    mDirection.Y = MOVE_DOWN;

                }



                if (Position.Y > mStartingPosition.Y)
                {

                    Position.Y = mStartingPosition.Y;

                    mCurrentState = State.Walking;

                    mDirection = Vector2.Zero;

                }

            }

        }
        private void Jump()
        {

            if (mCurrentState != State.Jumping)
            {

                mCurrentState = State.Jumping;

                mStartingPosition = Position;

                mDirection.Y = MOVE_UP;

                mSpeed = new Vector2(WIZARD_SPEED, WIZARD_SPEED);

            }

        }

        private void UpdateDuck(KeyboardState aCurrentKeyboardState)
        {
            if (aCurrentKeyboardState.IsKeyDown(Keys.A) == true)
            {
                Duck();
                


            }
            else
            {
                StopDucking();
            }
        }

        private void Duck()
        {

            if (mCurrentState == State.Walking)
            {
                mSpeed = Vector2.Zero;
                mDirection = Vector2.Zero;
                Source = new Rectangle(150, 0, 150, Source.Height);
                mCurrentState = State.Ducking;
            }
            
        }
        private void StopDucking()
        {

            if (mCurrentState == State.Ducking)
            {
                Source = new Rectangle(0, 0, 150, Source.Height);

                mCurrentState = State.Walking;

            }

        }

        public override void Draw(SpriteBatch theSpriteBatch)
        {

            foreach (hibou2.Fireball aFireball in mFireballs)
            {
                aFireball.Draw(theSpriteBatch);
            }
            base.Draw(theSpriteBatch);

        }




                
    }
    
}
